package com.hitsoyauce.mail.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import com.hitsoyauce.common.BASE64;
import com.hitsoyauce.common.PreferenceHelper;
import com.hitsoyauce.common.Util;

public class SettingWindow extends JDialog implements ActionListener, WindowListener {
	private static final long serialVersionUID = 101285272564330044L;

	private JButton btnOk;
	private JButton btnCancel;
	private JLabel lblName;
	private JLabel lblMail;
	private JLabel lblMailContext;
	private JLabel lblPwd;
	private JLabel lblSendTo;
	private JTextField txtName;
	private JPasswordField txtPwd;
	private JTextField txtSendTo;
	private JLabel lblMobile;
	private JTextField txtMobile;
	private MainWindow parent;
	
	public SettingWindow(final MainWindow parent) {
		this.parent = parent;
		getContentPane().setLayout(null);
		setTitle("设置");
		setResizable(false);
		setSize(321, 272);
		setLocation();
		setVisible(true);
		
		btnOk = new JButton("确定");
		btnOk.setFont(new Font("宋体", Font.PLAIN, 12));
		btnOk.addActionListener(this);
		btnCancel = new JButton("取消");
		btnCancel.setFont(new Font("宋体", Font.PLAIN, 12));
		btnCancel.addActionListener(this);
		
		lblName = new JLabel("姓名：");
		lblName.setFont(new Font("宋体", Font.PLAIN, 12));
		lblMail = new JLabel("邮件地址：");
		lblMail.setFont(new Font("宋体", Font.PLAIN, 12));
		lblMailContext = new JLabel(PreferenceHelper.getPreference("mailFrom"));
		lblMailContext.setFont(new Font("宋体", Font.PLAIN, 12));
		lblPwd = new JLabel("账户密码：");
		lblPwd.setFont(new Font("宋体", Font.PLAIN, 12));
		lblMobile = new JLabel("手机：");
		lblMobile.setFont(new Font("宋体", Font.PLAIN, 12));
		lblSendTo = new JLabel("发送至：");
		lblSendTo.setFont(new Font("宋体", Font.PLAIN, 12));
		
		txtName = new JTextField(PreferenceHelper.getPreference("name"));
		txtName.setColumns(10);
		txtMobile = new JTextField(PreferenceHelper.getPreference("phone"));
		txtMobile.setColumns(10);
		txtPwd = new JPasswordField();
		txtPwd.setText(BASE64.decryptBASE64(PreferenceHelper.getPreference("password")));
		txtSendTo = new JTextField(PreferenceHelper.getPreference("mailTo"));
		txtSendTo.setColumns(10);
		
		btnOk.setBounds(63, 194, 85, 30);
		btnCancel.setBounds(176, 194, 85, 30);
		lblName.setBounds(57, 28, 36, 20);
		lblMail.setBounds(33, 58, 60, 20);
		lblMailContext.setBounds(103, 58, 180, 20);
		lblPwd.setBounds(33, 88, 60, 20);
		txtName.setBounds(103, 28, 156, 21);
		lblMobile.setBounds(57, 122, 36, 15);
		txtMobile.setBounds(103, 119, 156, 21);
		txtPwd.setBounds(103, 88, 156, 20);
		lblSendTo.setBounds(44, 153, 48, 20);
		txtSendTo.setBounds(103, 153, 156, 21);
		
		getContentPane().add(btnOk);
		getContentPane().add(btnCancel);
		getContentPane().add(lblName);
		getContentPane().add(lblMail);
		getContentPane().add(lblMailContext);
		getContentPane().add(txtName);
		getContentPane().add(lblMobile);
		getContentPane().add(txtMobile);
		getContentPane().add(txtPwd);
		getContentPane().add(lblPwd);
		getContentPane().add(lblSendTo);
		getContentPane().add(txtSendTo);
		
		txtName.addCaretListener(new CaretListener() {
			@Override
			public void caretUpdate(CaretEvent e) {
				String py = getPinyinName(txtName.getText());
				lblMailContext.setText(py + "@nj-strongit.com.cn");
			}
		});
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				parent.setEnabled(true);
				return;
			} 
		});
		
	}
	
	private void setLocation() {
		int x = (int) (parent.getLocation().getX() + (parent.getSize().getWidth() - this.getSize().getWidth()) / 2);
		int y = (int) (parent.getLocation().getY() + (parent.getSize().getHeight() - this.getSize().getHeight()) / 2);
		setLocation(x, y);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//确认
		if (e.getSource().equals(btnOk)) {
			PreferenceHelper.setPreference("name", txtName.getText());
			PreferenceHelper.setPreference("mailFrom", lblMailContext.getText());
			PreferenceHelper.setPreference("phone", txtMobile.getText());
			PreferenceHelper.setPreference("username", getPinyinName(txtName.getText()));
			PreferenceHelper.setPreference("password", BASE64.encryptBASE64(new String(txtPwd.getPassword())));
			PreferenceHelper.setPreference("mailTo", txtSendTo.getText());
			
			parent.refreshPrev();
			parent.setEnabled(true);
			dispose();
		}
		
		//取消
		if (e.getSource().equals(btnCancel)) {
			parent.setEnabled(true);
			this.dispose();
		}
	}

	private String getPinyinName(String name) {
		String py;
		if (name.length() > 2) {
			py = Util.getPinYin(name.substring(0, 1));
			py += Util.getPinYinHeadChar(name.substring(1));
		} else {
			py = Util.getPinYin(name);
		}
		return py;
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		requestFocus();
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
}
